<template>
  <div class="app-container">

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          plain
          @click="handleAdd"
        >新增</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <vxe-table 
      v-loading="loading" 
      :data="AssetCategoryList" 
      @checkbox-change="handleSelectionChange"
      border
      stripe>
      
      <vxe-column type="checkbox" width="55" />
      <vxe-column 
        field="number" 
        title="分类编码" 
        align="center" 
        v-if="columns[0].visible" />
      <vxe-column 
        field="name" 
        title="分类名称" 
        align="center" 
        v-if="columns[1].visible" />
      <vxe-column 
        field="parentId" 
        title="父节点ID" 
        align="center" 
        v-if="columns[2].visible" />
      <vxe-column 
        field="orderNo" 
        title="显示排序" 
        align="center" 
        v-if="columns[3].visible" />
      <vxe-column 
        field="remark" 
        title="备注" 
        align="center" 
        v-if="columns[4].visible" />
      <vxe-column title="操作" align="center" width="150">
        <template v-slot:default="{ row }">
          <el-button
            type="primary"
            link
            icon="el-icon-edit"
            @click="handleUpdate(row)"
          >修改</el-button>
          <el-button
            type="primary"
            link
            icon="el-icon-delete"
            @click="handleDelete(row)"
          >删除</el-button>
        </template>
      </vxe-column>
    </vxe-table>

 <div class="pagination-container">
  <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
 </div>
    
    <edit ref="editDialog" @sub="getList"></edit>

  </div>
</template>

<script>
import { listAssetCategory, getAssetCategory, delAssetCategory, exportAssetCategory } from "./api.js";
import Edit from "./edit";

export default {
  components: {Edit},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 资产分类表表格数据
      AssetCategoryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      parentNode: {},
      parentName: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        number: null,
        name: null,
        parentId: null,
        orderNo: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "分类名称不能为空", trigger: "blur" }
        ],
      },
      columns: [
            { key: 1, label: "分类编码", visible:  true  },
            { key: 2, label: "分类名称", visible:  true  },
            { key: 3, label: "父节点ID", visible:  true  },
            { key: 4, label: "显示排序", visible:  true  },
            { key: 5, label: "备注", visible:  true  },
             ],
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    /** 查询资产分类表列表 */
    getList() {
      this.loading = true;
      const {pageNum, pageSize} = this.queryParams;
      
      const query = {...this.queryParams, pageNum, pageSize};
      const pageReq = {page: pageNum - 1, size: pageSize};
      listAssetCategory(query, pageReq).then(response => {
        const { content, totalElements } = response
        this.AssetCategoryList = content;
        this.total = totalElements;
        this.loading = false;
      }).catch(error => {
        this.loading = false;
        console.log(error);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 修改父节点
    changeParent(parent) {
      this.parentNode = parent;
      this.parentName = parent.name;
      this.queryParams.parentNumber = parent.number;
      this.getList();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        number: null,
        name: null,
        parentId: null,
        orderNo: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {

      this.$refs.editDialog.open({
        parentNode: this.parentNode
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAssetCategory(id).then(response => {
        this.form = response;
        this.open = true;
        this.title = "修改资产分类表";
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除资产分类表编号为"' + ids + '"的数据项？').then(function() {
        return delAssetCategory(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有资产分类表数据项？').then(() => {
        this.exportLoading = true;
        return exportAssetCategory(queryParams);
      }).then(response => {
        this.$download.download(response);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>

<style lang="scss" scoped>
.app-container {
  display: flex;
  flex-direction: column;
  height: 92vh; 
  overflow: hidden; 
}

.vxe-table {
  flex: 1;
  overflow: auto;
}

.pagination-container {
  display: flex;
  justify-content: flex-end; 

}
</style>

